<?php

namespace Common\Model;

/**
 * 房型上架信息表
 * @index roomtypepicture` (`roomtype_id`)
 */
class RoomTypePictureModel extends BaseModel {

    protected $trueTableName = 'bk_room_type_picture';

    /**
     * 根据房型id获取
     * @modify trainyao 2016-04-29  创建
     * @param  mixin   $roomTypeId 客栈ids
     * @return array               房型数据
     */
    public function getInfoByRoomType($roomTypeId) {
        $option = array();

        if (is_array($roomTypeId)) {
            $roomTypeId = implode(',', $roomTypeId);
        }
        if (strpos($roomTypeId, ',') === false) {
            $option['roomtype_id'] = $roomTypeId;
        } else {
            $option['roomtype_id'] = array('in', $roomTypeId);
        }
        $option['isdelete'] = NOT_DELETE;
        
        return $this->where($option)->select();
    }

    /**
     * 增加一个或多个客栈介绍图片
     * @modify trainyao 2016-05-03  创建
     * @param  integer   $hotelId    客栈ID
     * @param  integer   $roomTypeId 房型ID
     * @param  array   $url        图片URL信息数组
     * @return boolean              执行结果
     */
    public function addInfo($roomTypeId, $url) {
        $creator = getUserId();
        $data = array();
        foreach ($url as $urlItem) {
            array_push($data, array(
                'roomtype_id' => $roomTypeId,
                'name' => $urlItem,
                'creator' => $creator,
                'operator' => $creator,
            ));
        }
        unset($urlItem);

        return $this->addAll($data) == false ? false : true;
    }

    /**
     * 根据一个或多个房型ID删除房型图片数据
     * @modify trainyao 2016-01-04 创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $roomTypePictureId    图片id(s)
     * @return boolean 执行结果
     */
    public function deleteInfo($roomTypePictureId) {
        $option = array();

        if(is_array($roomTypePictureId)) {
            $roomTypePictureId = implode(',', $roomTypePictureId);
        }
        if(strpos($roomTypePictureId, ',') !== false) {
            $option['id'] = array('in', $roomTypePictureId);
        } else {
            $option['id'] = $roomTypePictureId;
        }
        
        $option['isdelete'] = NOT_DELETE;


        $data = array(
            'isdelete' => IS_DELETE,
        );

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 根据房型id，url 获取图片Id
     * @modify wenhui 2016-11-29  创建
     * @param  int   $roomTypeId 房型id
     * @param  string   $url 
     * @return array               房型数据
     */
    public function getImageInfo($roomTypeId, $url) {
        $option = array();

        if (is_null($roomTypeId) || is_null($url)) {
            return false;
        }
        $option['roomtype_id'] = $roomTypeId;
        $option['name'] = $url;
        $option['isdelete'] = NOT_DELETE;

        return $this->where($option)->find();
    }
}
